/**
 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * fills arr in-place by specified value with respect to passed indexes
 *
 * @param arr source array to be filled
 *
 * @param value a value to fill arr with
 *
 * @param startIndex index of arr to start from
 *
 * @param endIndex last index of arr, exclusive, i.e. arr[endIndex] is not changed
 *
 * @example: fill all content of arr with value
 * ```
 * fill(arr, value, 0, arr.length)
 * ```
 */
export function fill(arr: {{T}}[], value: {{T}}, startIndex: int, endIndex: int): void throws {
    if (!checkRange(arr.length, startIndex, endIndex)){
        throw new ArrayIndexOutOfBoundsException("fill: bounds verification failed")
    }

    for (let i: int = startIndex; i < endIndex; i++) {
        arr[i] = value;
    }
}

/**
 * fills arr in-place by specified value
 *
 * @param arr source array to be filled
 *
 * @param value a value to fill arr with
 *
 * @example: fill all content of arr with value
 * ```
 * fill(arr, value)
 * ```
 */
export function fill(arr: {{T}}[], value: {{T}}): void {
    try {
        fill(arr, value, 0, arr.length);
    } catch (e) {
        // TODO(ivan-tyulyandin): code below is an overcheck, but will be helpful in case of strange exceptions
        assert false : "fill: should be unreacheable since indicies have to be correct by design, " + e.toString()
    }
}
